import java.util.ArrayList;
import java.util.Iterator;

public abstract class Point {
	FlowerGrid grid;
	public Point(FlowerGrid grid) {
		this.grid = grid;
	}
	public Location getLocation() {
		return grid.getLocation(this);
	}
	private Location[] modifiers = {
			new Location(0,1),
			new Location(0,-1),
			new Location(1,0),
			new Location(-1,0),
			new Location(1,-1),
			new Location(-1,1)
	};
	public Point[] getNeighboringPoints() {
		Point[] ret = new Point[6];
		Location myLoc = getLocation();
		int pos=0;
		for(int i=0;i<6;i++) {
			Point p = grid.get(myLoc.plus(modifiers[i]));
			if(p!=null) {
				ret[i]=p;
				pos++;
			}
		}
		Point[] ret2 = new Point[pos];
		System.arraycopy(ret,0,ret2,0,pos-1);
		return ret;
	}
	public Resource[] getNeighboringResources() {
		return (Resource[]) getNeighboringOfClass(Resource.class);
	}
	public Constructable[] getNeighboringConstructables() {
		return (Constructable[]) getNeighboringOfClass(Constructable.class);
	}
	protected Point[] getNeighboringOfClass(Class<?> c) {
		Point[] points = getNeighboringPoints();
		Point[] r1 = new Point[6];
		int i=0;
		for(Point p : points) {
			if(c.isAssignableFrom(p.getClass())) {
				r1[i]=p;
				i++;
			}
		}
		Point[] r2 = new Point[i-1];
		System.arraycopy(r1, 0, r2, 0, i-1);
		return r2;
	}
}
